package com.gym.controller.form;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

public class FormLogin extends ActionForm {
	
	private String email;
	private String password;
	
	public void setEmail(String email) {
		this.email = email;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getEmail() {
		return email;
	}
	
	public String getPassword() {
		return password;
	}
	
	@Override
	public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {

		ActionErrors errors = new ActionErrors();
		
		if (email==null || email.isEmpty()) 
			errors.add("email", new ActionMessage("it.errore.email.obbligatiorio"));
		else if( ! email.matches("[-0-9a-zA-Z.+_]+@[-0-9a-zA-Z.+_]+.[a-zA-Z]{2,4}"))
			errors.add("email", new ActionMessage("it.errore.email.formato"));
		
		if (password==null || password.isEmpty()) {
			errors.add("password", new ActionMessage("it.errore.password.obbligatiorio"));
		} else if (password.length() < 8) {
			errors.add("password", new ActionMessage("it.errore.password.corta"));
		}
		
		return errors;
	}
}
